<template>
  <div>
    <el-button type="primary" @click="dialogVisible = true">添加学生</el-button>
    <el-table :data="students" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="className" label="班级" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button type="danger" size="small" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加学生" v-model="dialogVisible">
      <el-form :model="form">
        <el-form-item label="姓名">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="班级">
          <el-input v-model="form.className" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="add">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getAllStudents, addStudent, deleteStudent } from '@/api/mbti';

export default {
  data() {
    return {
      students: [],
      dialogVisible: false,
      form: { name: '', className: '' }
    };
  },
  mounted() {
    this.load();
  },
  methods: {
    async load() {
      const res = await getAllStudents();
      this.students = res.data;
    },
    async add() {
      await addStudent(this.form);
      this.dialogVisible = false;
      this.load();
    },
    async remove(id) {
      await deleteStudent(id);
      this.load();
    }
  }
};
</script>
